﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Collections;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace Bixas.Tangibilis.Controls.Primitives
{
    internal class TouchButtonBaseAdapter<T> : TouchUIElementAdapter<T>
        where T : ButtonBase, ITouchElement
    {

        public TouchButtonBaseAdapter(T elementAdapted)
            : base(elementAdapted)
        { }

        protected virtual void OnTouchClick(RawTouchCursorInputEventArgs e)
        {
            ButtonBase element = ((ButtonBase)this.TouchElement);
            RoutedEventArgs args = new RoutedEventArgs(ButtonBase.ClickEvent, this);
            element.RaiseEvent(args);
            //CommandHelpers.ExecuteCommandSource(this);
            Utility.CommandHelperWrapper.ExecuteCommandSource(element);
        }

        public override void OnTouchCursorDown(RoutedEventArgs e)
        {
            base.OnTouchCursorDown(e);
            RawTouchCursorInputEventArgs args = e as RawTouchCursorInputEventArgs;
            if (args != null)
            {
                //args.Handled = true;
                this.GetStateObject(args.Cursor.cursorID).IsPressed = true;
                this.TouchElement.IsTouchCursorPressed = true;
                //args.Cursor.CaptureElement(this.TouchElement);
                //ITouchCursorEvent element = this.TouchElement as ITouchCursorEvent;
                //RoutedEventArgs args2 = new RoutedEventArgs(TouchCursorEvents.TouchCursorDownEvent, element);

                //((UIElement)this.TouchElement).RaiseEvent(args2);
            }
        }
        public override void OnTouchCursorUp(System.Windows.RoutedEventArgs e)
        {
            RawTouchCursorInputEventArgs args = (RawTouchCursorInputEventArgs)e;
            TouchCursorState stateObject = this.GetStateObject(args.Cursor.cursorID);
            if (stateObject.IsPressed)
            {
                OnTouchClick((RawTouchCursorInputEventArgs)e);
            }
            args.Cursor.ReleaseCapture();
            base.OnTouchCursorUp(e);

        }
    }

}
